﻿using Furion.FriendlyException;
using Models.Mailbox;
using SqlSugar;

namespace Web.Mapper.Impl
{
    public class MailboxConfigureMapperImpl : MailboxConfigureMapper
    {

        public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = "User Id=sjadmin;Password=sjjt62913888;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.8.1.121)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=adminnet)))",//连接符字串
            DbType = DbType.Oracle,//数据库类型
            IsAutoCloseConnection = true //不设成true要手动close
        },
            db => {
                //(A)全局生效配置点，一般AOP和程序启动的配置扔这里面 ，所有上下文生效
                //调试SQL事件，可以删掉
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响

                    //获取原生SQL推荐 5.1.4.63  性能OK
                    //UtilMethods.GetNativeSql(sql,pars)

                    //获取无参数化SQL 对性能有影响，特别大的SQL参数多的，调试使用
                    //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)

                };

            });
    


         public  int AddMailboxConfigAsync(MailboxConfigure mailbox)
        {
            try
            {
                //开启事务
                Db.Ado.BeginTran();
                int cnt =  Db.Insertable<MailboxConfigure>(mailbox).ExecuteCommand();

                if (cnt == 0)
                {
                    throw Oops.Oh($"新建任务失败！");
                }
                //提交事务
                Db.Ado.CommitTran();
                return cnt;
            }
            catch (Exception e)
            {
                //回滚事务
                Db.Ado.RollbackTran();
                throw Oops.Oh($"异常：{e.Message}");
            }

        }
    }
}

